package com.enterprise.controller.manage;

import com.enterprise.cache.FrontCache;
import com.enterprise.controller.BaseController;
import com.enterprise.entity.CaseType;
import com.enterprise.service.CaseTypeService;
import com.enterprise.service.Services;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/manage/caseType/")
public class CaseTypeAction extends BaseController<CaseType>{
	
	private static final String page_toList = "/manage/caseType/caseTypeList";
	private static final String page_toAdd = "/manage/caseType/caseTypeEdit";
	private static final String page_toEdit = "/manage/caseType/caseTypeEdit";

	
	public CaseTypeAction(){
		super.page_toList = page_toList;
		super.page_toAdd = page_toAdd;
		super.page_toEdit = page_toEdit;
	}

	@Autowired
	private CaseTypeService caseTypeService;
	@Autowired
    private FrontCache frontCache;
	
	@Override
	public String selectList(HttpServletRequest request,CaseType caseType){	
		List<CaseType> caseTypeList = caseTypeService.selectList(caseType);
		request.setAttribute("caseTypeList", caseTypeList);
		return page_toList;
		
	}
	
	@Override
	public Services<CaseType> getService() {
		// TODO Auto-generated method stub
		return caseTypeService;
	}
	
	
	@RequestMapping("unique")
    @ResponseBody
    public String unique(@ModelAttribute("e") CaseType e,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");//设置响应编码为utf-8
        if(StringUtils.isNotBlank(e.getName())){
        	CaseType caseType = new CaseType();
        	caseType.setName(e.getName());
        	caseType = caseTypeService.selectOne(caseType);

            if(caseType==null){
                return "{}";
            }else{
                if(e.getId()!=0 && e.getId()==caseType.getId()){
                    return "{}";
                }else {
                    return "{\"error\":\"分类名称已经存在!\"}";
                }
            }
        }
        return null;
    }

	@Override
	public String insert(HttpServletRequest request , @ModelAttribute("e") CaseType e,RedirectAttributes flushAttrs) throws Exception{
		getService().insert(e);
		insertAfter(e);
		addMessage(flushAttrs,"操作成功！");
		frontCache.loadCaseType();;//加载缓存
		return "redirect:selectList";
	}
	
	 public String update(HttpServletRequest request, @ModelAttribute("e") CaseType e, RedirectAttributes flushAttrs) throws Exception {
	        getService().update(e);
	        insertAfter(e);
	        addMessage(flushAttrs, "操作成功！");
	        frontCache.loadCaseType();;//加载缓存
	        return "redirect:selectList";
	    }
	
	 public String toDel(@ModelAttribute("e") CaseType e ,RedirectAttributes flushAttrs) throws Exception{
			getService().delete(e);
			addMessage(flushAttrs,"操作成功！");
			frontCache.loadCaseType();;//加载缓存
			return "redirect:selectList";
		}
	
	
}
